#! -*-coding: utf-8 -*-
from django.conf import settings
try:
    from django.contrib.auth import get_user_model
except ImportError: # django < 1.5
    from django.contrib.auth.models import User
else:
    User = get_user_model()

from django import forms
from django.utils.translation import ugettext_lazy as _

from registration.backends import get_module
from registration.forms import *
class RegistrationFormUniqueEmailName(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which enforces uniqueness of
    email addresses and username.

    Written by cyber_lis for djangominesite project
    """
    error_messages = {
        'duplicate_username': _("A user with that username already exists."),
    }
    def clean_username(self):
        # Since User.username is unique, this check is redundant,
        # but it sets a nicer error message than the ORM. See #13147.
        username = self.cleaned_data["username"]
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])